package com.hairline.aspect;

import com.hairline.bean.Log;
import com.hairline.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class AdminLoggerAspect {

    @Autowired
    LogService logService;

    private static String username;

    @Pointcut("@annotation(com.hairline.aspect.AdminOperationLog)")
    public void adminLogPointcut() {}

    @Around("adminLogPointcut()")
    public Object AroundLog(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入切面");
        //获取请求对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getName();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = null;
        if (attributes != null) {
            request = attributes.getRequest();
        }

        //执行处理请求的方法
        Object proceed = null;
        boolean isSuccessful = true;
        try {
            proceed = joinPoint.proceed();
        } catch (Exception e) {
            e.printStackTrace();
            isSuccessful = false;
        }
        if ("login".equals(methodName)) {
            username = (String) request.getSession().getAttribute("username");
        }

        //记录到日志
        Log log = new Log();
        log.setAdmin(username);
        log.setIp(request.getRemoteAddr());
        log.setStatus(isSuccessful);
        if (!isSuccessful) {
            log.setComment("操作失败");
        } else {
            log.setComment("操作成功");
        }
        switch (signature.getName()) {
            case "login":
                log.setAction("登录");
                log.setType(1);
                log.setResult(username + "登录成功");
                break;
            case "logout":
                log.setAction("退出");
                log.setType(1);
                log.setResult(username + "注销登录");
                break;
            case "adminCreate":
                log.setAction("添加管理员");
                log.setType(0);
                log.setResult("添加成功 " + username);
                break;
            case "adminUpdate":
                log.setAction("修改管理员");
                log.setType(0);
                log.setResult("修改成功 " + username);
                break;
            case "adminDelete":
                log.setAction("删除管理员");
                log.setType(0);
                log.setResult("(DELETED)");
                break;
            default:
                log.setAction("未知操作");
                log.setType(0);
        }
        //记录log到数据库
        logService.setLog(log);

        return proceed;
    }
}